/*
 * FileSetPanel.java
 *
 * Created on March 27, 2005, 1:07 AM
 */

package tools;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

/**
 *
 * @author Keith
 */
public class FileSetPanel extends JPanel {
  
  transient private ArrayList<ActionListener> actionListeners = new ArrayList<ActionListener>();

	final static private int DEFAULT_FILENAME_LENGTH = 30;

	private JTextField watchListFileNameFld;
	private JTextField webExtractorFileNameFld;
	private JTextField bivioDataFileNameFld;
	private JTextField reportDefinitionFileNameFld;

	private MultipleDocumentAction proceedAction;
	private JButton proceedButton;

	/**
	 *
	 */
  public FileSetPanel() {
		setLayout( new BorderLayout() );
		setName( "File Set" );

		Icon icon = ApplicationConfiguration.getInstance().getIcon( "play" );

	  proceedAction = new MultipleDocumentAction( "Proceed", icon ){
			public void actionPerformed( ActionEvent evt ){
				fireActionPerformed( evt );
			}
		};

		add( createFieldPanel(), BorderLayout.CENTER );

		add( createButtonPanel(), BorderLayout.SOUTH );

		proceedAction.addDocument( bivioDataFileNameFld.getDocument() );
		proceedAction.addDocument( webExtractorFileNameFld.getDocument() );
		proceedAction.addDocument( reportDefinitionFileNameFld.getDocument() );
//		proceedAction.addDocument( watchListFileNameFld.getDocument() );
  }

	private JPanel createButtonPanel(){
		JPanel p = new JPanel( new FlowLayout( FlowLayout.CENTER, 5, 5 ) );

		proceedButton = new JButton( proceedAction );

		p.add( proceedButton );

		return p;
	}

	private JPanel createFieldPanel(){
		JPanel p = new JPanel( new GridBagLayout() );
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.anchor = gbc.WEST;
		gbc.fill = gbc.NONE;

		gbc.gridx = 0;
		gbc.gridy = 0;
		watchListFileNameFld = addPromptField( p, gbc, "Watch List", 'W',
									  ApplicationConfiguration.xmlFilter );

		gbc.gridx = 0;
		webExtractorFileNameFld = addPromptField( p, gbc, "Web Extractor", 'E',
									  ApplicationConfiguration.xmlFilter );

		gbc.gridx = 0;
		bivioDataFileNameFld = addPromptField( p, gbc, "Bivio Data", 'B',
									  ApplicationConfiguration.xmlFilter );

		gbc.gridx = 0;
		reportDefinitionFileNameFld = addPromptField( p, gbc,
									  "Report Definition", 'R',
									  ApplicationConfiguration.xslFilter );

		return p;
	}

	static private JTextField addPromptField( JPanel p, GridBagConstraints gbc, String prompt,
																char mnemonic,
																javax.swing.filechooser.FileFilter filter ){
		JTextField fldComp = new JTextField( DEFAULT_FILENAME_LENGTH );
		JLabel label = new JLabel( prompt );
		label.setBorder( javax.swing.BorderFactory.createEtchedBorder() );
		gbc.gridy++;
		p.add( label, gbc );

		gbc.gridx++;
		label.setLabelFor( fldComp );

		// This border is required to prevent 'W's from being "left" truncated.
		Border b = BorderFactory.createEmptyBorder( 2, 4, 2, 2 );
		label.setBorder( b );

		label.setDisplayedMnemonic( mnemonic );
		p.add( fldComp, gbc );

		gbc.gridx++;
		JFileChooser jf = ApplicationConfiguration.getInstance().getFileChooser();
		JButton jb = new JButton( new BrowseAction( jf, fldComp.getDocument(), filter ) );
		p.add( jb, gbc );

		return fldComp;
	}

  public String getBivioDataFileName() {
    return bivioDataFileNameFld.getText();
  }

  public void setBivioDataFileName(String bivioDataFileName){
    bivioDataFileNameFld.setText( bivioDataFileName );
  }

  public String getReportDefinitionFileName() {
    return reportDefinitionFileNameFld.getText();
  }

  public void setReportDefinitionFileName(String reportDefinitionFileName){
    reportDefinitionFileNameFld.setText( reportDefinitionFileName );
  }

  public String getWatchListFileName() {
    return watchListFileNameFld.getText();
  }

  public void setWatchListFileName(String watchListFileName){
    watchListFileNameFld.setText( watchListFileName );
  }

  public String getWebExtractorFileName() {
    return webExtractorFileNameFld.getText();
  }

  public void setWebExtractorFileName(String webExtractorFileName){
    webExtractorFileNameFld.setText( webExtractorFileName );
  }

  public void removeActionListener( ActionListener l ) {
    synchronized ( actionListeners ){
      if ( actionListeners.contains( l ) ) {
        actionListeners.remove( l );
      }
    }
  }

  public void addActionListener( ActionListener l ) {
    synchronized ( actionListeners ){
      if ( ! actionListeners.contains( l ) ) {
        actionListeners.add( l );
      }
    }
  }

  protected void fireActionPerformed( ActionEvent e ) {
    ActionListener[] listeners = null;

    synchronized ( actionListeners ){
			int size = actionListeners.size();

			if ( size > 0 ){
				listeners = new ActionListener[ size ];

				actionListeners.toArray( listeners );
			}
    }

		ActionEvent ae = new ActionEvent( this, e.ACTION_PERFORMED,
                                            e.getActionCommand() );

    int count = listeners != null ? listeners.length : 0;
    for ( int i = 0; i < count; i++ ) {
      listeners[ i ].actionPerformed( ae );
    }
  }
}
